#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<string> ret;
    string path;
    vector<string> generateParenthesis(int n) {
        dfs(n, 0, 0);
        return ret;
    }
    void dfs(int n, int left, int right)
    {
        if (right == n)
        {
            ret.push_back(path);
            return;
        }

        if (left < n)
        {
            path.push_back('(');
            dfs(n, left + 1, right);
            path.pop_back();
        }
        if (right < left)
        {
            path.push_back(')');
            dfs(n, left, right + 1);
            path.pop_back();
        }
    }
};